<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use think\Response;
use think\response\Json;
use app\common\model\News as NewsModel;

class News extends Base
{
    /**
     * 公告详情
     */
    public function info()
    {
        $param = $this->request->param();
        try {
            $id = $param['id'];
            $info = NewsModel::where('id', $id)->find();
            if (empty($info)) {
                return Result::fail('记录不存在！');
            }
            $url = 'http://' . $_SERVER['SERVER_NAME'] . '/uploads/';
            $info->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $info->content));
            $info->content = str_replace('\\', '', $info->content);
            return Result::success('获取成功！',$info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 公告列表
     */
    public function getList(): Response
    {
        $param = $this->request->param();
        try {
            $list = NewsModel::getPageList([], 'id,title,images,create_time', $param['page'], 20);
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
